import Vue from 'vue'
import Vuex from 'vuex'
import $http from '../http/index.js'
import $api from '../http/api.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('token') || null, // 用户token
		searchHistoryList: uni.getStorageSync('searchHistoryList') || [], // 搜索历史
		homeChoosedDateObj: uni.getStorageSync('homeChoosedDateObj') || {}, // 首页班车日期选择
		homeChoosedAddressObj: uni.getStorageSync('homeChoosedAddressObj') || {}, // 班车地址选择
		curLocation: uni.getStorageSync('curLocation') || {}, // 当前定位信息
		choosedAddressObj: uni.getStorageSync('choosedAddressObj') || {}, // 用户选择出发地址
		choosedDriverObj: uni.getStorageSync('choosedDriverObj') || {}, // 用户选择意向司机
		pickuperChoosedAddressObj: uni.getStorageSync('pickuperChoosedAddressObj') || {}, //接送人选择的出发地址
		riderList: uni.getStorageSync('riderList') || [], // 乘车人列表
		pickuperList: uni.getStorageSync('pickuperList') || [], // 接送人列表
		pickuperListToAndFor: uni.getStorageSync('pickuperListToAndFor') || {}, // 往返接送人列表
		curPickuperInfo: uni.getStorageSync('curPickuperInfo') || [], // 当前所选接送人信息
		seatList: uni.getStorageSync('seatList') || [], // 所选座位列表
		seatListObj: uni.getStorageSync('seatListObj') || {}, // 所选座位列表 往返
		choosedInvoice: uni.getStorageSync('choosedInvoice') || '', // 用户选择发票
		orderType: uni.getStorageSync('orderType') || '', // 用户选择发票
		choosedBusCoupon: uni.getStorageSync('choosedBusCoupon') || {}, // 选择的班车优惠券
		choosedRentCity: uni.getStorageSync('choosedRentCity') || {}, // 选择的租车城市
		choosedRentCityStation: uni.getStorageSync('choosedRentCityStation') || {}, // 选择的租车城市站点
		rentInfo: uni.getStorageSync('rentInfo') || {}, // 租车选择信息（地址，时间）
		pinNumber: uni.getStorageSync('pinNumber') || 1, // 	拼车起订人数
		orderPayType: uni.getStorageSync('orderPayType') || '', //  需要支付的订单的类型状态 （用做微信追加支付）
		inviteCode: uni.getStorageSync('inviteCode') || '', //  上级邀请码
		isLogin: false, //  是否需要登录
		isCouponTan: true, //  是否需要弹出领取优惠券
		isCheck: false, // 版本
		www: 'https://imgs.2855855.com/', // 网络静态图片路径
		wwwImgUrl: '', // 网络图片路径
		changeNowCity:  uni.getStorageSync('changeNowCity') || '', // 切换的当前城市
		customerInfo:  uni.getStorageSync('customerInfo') || {}, // 用户积分信息
		jingqulist:  uni.getStorageSync('jingqulist') || {}, // 插入的景区列表
		jiudianlist:  uni.getStorageSync('jiudianlist') || {}, // 插入的酒店列表
		shoplist:  uni.getStorageSync('shoplist') || {}, // 插入的商品列表
		historylist: uni.getStorageSync('historylist') || [], // 文章发布的搜索列表历史
		choosedMakeCity: uni.getStorageSync('choosedMakeCity') || {}, // 定制选择的城市
		nowmyshop: uni.getStorageSync('nowmyshop') || [], // 购物车数据
		selected_list: uni.getStorageSync('selected_list') || [], // 购物车多个ids
	},

	mutations: {
		upToken(state, val) {
			uni.setStorageSync('token', val)
			state.token = val
		},
		// 退出 删除token
		delToken(state, val) {
			uni.removeStorageSync('token');
			state.token = null
		},
		upSearchHistoryList(state, val) {
			let arr = state.searchHistoryList
			if (!arr.includes(val)) {
				arr.unshift(val)
				uni.setStorageSync('searchHistoryList', arr)
				state.searchHistoryList = arr
			}
		},
		// 删除站点搜索历史
		delSearchHistoryList(state, val) {
			if (val || val === 0) {
				this.state.searchHistoryList.splice(val, 1)
				uni.setStorageSync('searchHistoryList', this.state.searchHistoryList)
			} else {
				uni.removeStorageSync('searchHistoryList');
				state.searchHistoryList = []
			}
		},
		upHomeChoosedDateObj(state, val) {
			let obj = state.homeChoosedDateObj
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('homeChoosedDateObj', obj)
			state.homeChoosedDateObj = obj
		},
		upHomeChoosedAddressObj(state, val) {
			let obj = state.homeChoosedAddressObj
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('homeChoosedAddressObj', obj)
			state.homeChoosedAddressObj = obj
		},
		delHomeChoosedDateObj(state, val) {
			uni.removeStorageSync('homeChoosedDateObj');
			state.homeChoosedDateObj = {}
		},
		delHomeChoosedAddressObj(state, val) {
			uni.removeStorageSync('homeChoosedAddressObj');
			state.homeChoosedAddressObj = {}
		},
		upCurLocation(state, val) {
			uni.setStorageSync('curLocation', val)
			state.curLocation = val
		},
		upChoosedAddressObj(state, val) {
			let obj = state.choosedAddressObj
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('choosedAddressObj', obj)
			state.choosedAddressObj = obj
		},
		delChoosedAddressObj(state, val) {
			uni.removeStorageSync('choosedAddressObj');
			state.choosedAddressObj = {}
		},
		upChoosedDriverObj(state, val) {
			let obj = state.choosedDriverObj
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('choosedDriverObj', obj)
			state.choosedDriverObj = obj
		},
		delChoosedDriverObj(state, val) {
			uni.removeStorageSync('choosedDriverObj');
			state.choosedDriverObj = {}
		},
		upPickuperChoosedAddressObj(state, val) {
			let obj = state.pickuperChoosedAddressObj
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('pickuperChoosedAddressObj', obj)
			state.pickuperChoosedAddressObj = obj
		},
		delPickuperChoosedAddressObj(state, val) {
			uni.removeStorageSync('pickuperChoosedAddressObj');
			state.pickuperChoosedAddressObj = {}
		},
		upRiderList(state, val) {
			let arr = state.riderList
			let stateRiderList_arr = []
			arr.forEach((it_r, in_r) => {
				stateRiderList_arr.push(it_r.id)
			})
			if (!stateRiderList_arr.includes(val.id)) { // 新增信息
				arr.unshift(val)
				uni.setStorageSync('riderList', arr)
				state.riderList = arr
			}
			// console.log('arr',arr);
			arr.forEach((item, index) => { // 修改信息
				if (item.id == val.id) {
					// console.log(item);
					arr[index] = val
					uni.setStorageSync('riderList', arr)
					state.riderList = JSON.parse(JSON.stringify(arr))
					// console.log(arr);
				}
			})
		},
		delRiderList(state, val) {
			if (val || val === 0) {
				this.state.riderList.splice(val, 1)
				uni.setStorageSync('riderList', this.state.riderList)
			} else {
				uni.removeStorageSync('riderList');
				state.riderList = []
			}
		},
		// 选取的接送人
		upPickuperList(state, val) {
			// val 是数组
			state.pickuperList = JSON.parse(JSON.stringify(val))
			uni.setStorageSync('pickuperList', JSON.parse(JSON.stringify(val)))
		},
		// 删除 单程接送人
		delPickuperList(state, val) {
			let arr = this.state.pickuperList
			if (val || val == 0) {
				arr.splice(val, 1)
				uni.setStorageSync('pickuperList', arr)
			} else {
				uni.removeStorageSync('pickuperList');
				state.pickuperList = []
			}
		},
		// 更新往返接送人列表
		upPickuperListToAndFor(state, val) {
			// val 是数组
			// state.pickuperListToAndFor = JSON.parse(JSON.stringify(val))
			// uni.setStorageSync('pickuperListToAndFor', JSON.parse(JSON.stringify(val)))
			
			let obj = state.pickuperListToAndFor
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('pickuperListToAndFor', obj)
			state.pickuperListToAndFor = obj
		},
		// 删除 往返接送人列表
		delPickuperListToAndFor(state, val) { // val = {type,item}
			if (!val) {
				uni.removeStorageSync('pickuperListToAndFor');
				state.pickuperListToAndFor = {}
				return
			}
			let obj = state.pickuperListToAndFor
			let arr = obj[val.type] || []
			let pickuper = val.item
			arr.forEach((item, i) => {
				if (item.id == pickuper.id) {
					arr.splice(i, 1)
				}
			})
			uni.setStorageSync('pickuperListToAndFor', obj)
		},
		// 本次所选接送人
		upCurPickuperInfo(state, val) {
			state.curPickuperInfo = JSON.parse(JSON.stringify(val))
			uni.setStorageSync('curPickuperInfo', JSON.parse(JSON.stringify(val)))
			// let arr = state.curPickuperInfo
			// let stateRiderList_arr = []
			// arr.forEach((it_r, in_r) => {
			// 	stateRiderList_arr.push(it_r.id)
			// })
			// if (!stateRiderList_arr.includes(val.id)) {
			// 	arr.unshift(val)
			// 	uni.setStorageSync('curPickuperInfo', arr)
			// 	state.curPickuperInfo = arr
			// } else {
			// 	arr.forEach((it_r, in_r) => {
			// 		if (it_r.id === val.id) {
			// 			arr[in_r] = val
			// 			uni.setStorageSync('curPickuperInfo', arr)
			// 			state.curPickuperInfo = arr
			// 		}
			// 	})
			// }
		},
		// 删除本次所选接送人
		delCurPickuperInfo(state, val) {
			let arr = this.state.curPickuperInfo
			if (val || val == 0) {
				arr.splice(val, 1)
				uni.setStorageSync('curPickuperInfo', arr)
			} else {
				uni.removeStorageSync('curPickuperInfo');
				state.curPickuperInfo = []
			}
		},
		upSeatList(state, val) {
			uni.setStorageSync('seatList', val)
			state.seatList = val
		},
		// 删除所选座位列表
		delSeatList(state, val) {
			uni.removeStorageSync('seatList');
			state.seatList = []
		},

		upSeatListObj(state, val) {
			let obj = state.seatListObj
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('seatListObj', obj)
			state.seatListObj = obj
		},
		delSeatListObj(state, val) {
			uni.removeStorageSync('seatListObj');
			state.seatListObj = {}
		},

		upChoosedInvoice(state, val) {
			uni.setStorageSync('choosedInvoice', val);
			state.choosedInvoice = val
		},
		delChoosedInvoice(state, val) {
			uni.removeStorageSync('choosedInvoice');
			state.choosedInvoice = ''
		},
		// 更新 订单类型
		upChoosedOrderType(state, val) {
			uni.setStorageSync('orderType', val);
			state.orderType = val
		},
		// 删除 订单类型
		delChoosedOrderType(state, val) {
			uni.removeStorageSync('orderType');
			state.orderType = ''
		},
		upChoosedBusCoupon(state, val) {
			uni.setStorageSync('choosedBusCoupon', val);
			state.choosedBusCoupon = val
		},
		delChoosedBusCoupon(state, val) {
			uni.removeStorageSync('choosedBusCoupon');
			state.choosedBusCoupon = {}
		},
		upChoosedRentCity(state, val) {
			let obj = state.choosedRentCity
			let k = Object.keys(val)
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('choosedRentCity', obj)
			state.choosedRentCity = JSON.parse(JSON.stringify(obj))
		},
		delChoosedRentCity(state, val) {
			uni.removeStorageSync('choosedRentCity');
			state.choosedRentCity = {}
		},
		upChoosedRentCityStation(state, val) {
			let obj = state.choosedRentCityStation
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('choosedRentCityStation', obj)
			state.choosedRentCityStation = JSON.parse(JSON.stringify(obj))
		},
		delChoosedRentCityStation(state, val) {
			uni.removeStorageSync('choosedRentCityStation');
			state.choosedRentCityStation = {}
		},
		upRentInfo(state, val) {
			uni.setStorageSync('rentInfo', val)
			state.rentInfo = val
		},
		delRentInfo(state, val) {
			uni.removeStorageSync('rentInfo');
			state.rentInfo = {}
		},
		// 更新	拼车起订人数
		upPinNumber(state, val) {
			uni.setStorageSync('pinNumber', val)
			state.pinNumber = val
		},
		// 更新	订单支付类型
		upOrderPayType(state, val) {
			uni.setStorageSync('orderPayType', val)
			state.orderPayType = val
		},
		// 删除	订单支付类型
		delOrderPayType(state, val) {
			uni.removeStorageSync('orderPayType');
			state.orderPayType = ''
		},
		// 更新	上级邀请码
		upInviteCode(state, val) {
			uni.setStorageSync('inviteCode', val)
			state.inviteCode = val
		},
		// 删除	上级邀请码
		delInviteCode(state, val) {
			uni.removeStorageSync('inviteCode')
			state.inviteCode = ''
		},
		// 更新	是否需要登录
		upIsLogin(state, val) {
			// uni.setStorageSync('isLogin', val)
			state.isLogin = val
		},
		// 更新	是否需要登录
		upIsCouponTan(state, val) {
			state.isCouponTan = val
		},
		// 更新	版本
		upIsCheck(state, val) {
			state.isCheck = val
		},
		// 更新切换当前城市
		upChangeNowCity(state, val) {
			uni.setStorageSync('changeNowCity', val);
			state.changeNowCity = val
		},
		// 更新用户积分信息
		upCustomerInfo(state, data) {
			uni.setStorageSync('customerInfo', data)
			state.customerInfo = data
		},
		// 更新选取的景区列表
		upJingqulist(state, data) {
			uni.setStorageSync('jingqulist', data)
			state.jingqulist = data
		},
		// 删除选取的景区列表
		delJingqulist(state, data) {
			uni.removeStorageSync('jingqulist')
			state.jingqulist = ''
		},
		// 更新选取的酒店列表
		upJiudianlist(state, data) {
			uni.setStorageSync('jiudianlist', data)
			state.jiudianlist = data
		},
		// 删除选取的酒店列表
		delJiudianlist(state, data) {
			uni.removeStorageSync('jiudianlist')
			state.jiudianlist = ''
		},
		// 更新选取的商品列表
		upShoplist(state, data) {
			uni.setStorageSync('shoplist', data)
			state.shoplist = data
		},
		// 删除选取的商品列表
		delShoplist(state, data) {
			uni.removeStorageSync('shoplist')
			state.shoplist = ''
		},
		// 更新文章发布的历史
		upHistorylist(state, data) {
			uni.setStorageSync('historylist', data)
			state.historylist = data
		},
		// 删除文章发布的历史
		delHistorylist(state, data) {
			uni.removeStorageSync('historylist')
			state.historylist = ''
		},
		// 更新 选择的定制的城市
		upChoosedMakeCity(state, val) {
			let obj = state.choosedMakeCity
			Object.keys(val).forEach((key) => {
				obj[key] = val[key]
			})
			uni.setStorageSync('choosedMakeCity', obj)
			state.choosedMakeCity = obj
		},
		// 删除 选择的定制的城市
		delChoosedMakeCity(state, val) {
			uni.removeStorageSync('choosedMakeCity');
			state.choosedMakeCity = {}
		},
		// 更新 选择的购物车列表
		upnowmyshop(state, data) {
			uni.setStorageSync('nowmyshop', data)
			state.nowmyshop = data
		},
		// 删除 选择的购物车列表
		delnowmyshop(state, data) {
			uni.removeStorageSync('nowmyshop')
			state.nowmyshop = ''
		},
		
		upselected_list(state, data) {
			uni.setStorageSync('selected_list', data)
			state.selected_list = data
		},
		// 删除 选择的定制的城市
		delselected_list(state, data) {
			uni.removeStorageSync('selected_list')
			state.selected_list = ''
		},
	},

	getters: {
		getToken(state) {
			return state.token
		},
		getSearchHistoryList(state) {
			return state.searchHistoryList
		},
		getHomeChoosedDateObj(state) {
			return state.homeChoosedDateObj
		},
		getHomeChoosedAddressObj(state) {
			return state.homeChoosedAddressObj
		},
		getCurLocation(state) {
			return state.curLocation
		},
		getChoosedAddressObj(state) {
			return state.choosedAddressObj
		},
		getChoosedDriverObj(state) {
			return state.choosedDriverObj
		},
		getPickuperChoosedAddressObj(state) {
			return state.pickuperChoosedAddressObj
		},
		getRiderList(state) {
			return state.riderList
		},
		getPickuperList(state) {
			return state.pickuperList
		},
		// 获取本次所选的接收人
		getCurPickuperInfo(state) {
			return state.curPickuperInfo
		},
		getSeatList(state) {
			return state.seatList
		},
		getSeatListObj(state) {
			return state.seatListObj
		},
		// 获取 往返接送人列表
		getPickuperListToAndFor(state) {
			return state.pickuperListToAndFor
		},
		// 获取订单号
		getChoosedInvoice(state) {
			return state.choosedInvoice
		},
		// 获取订单类型
		getChoosedOrderType(state) {
			return state.orderType
		},
		getChoosedBusCoupon(state) {
			return state.choosedBusCoupon
		},
		getChoosedRentCity(state) {
			return state.choosedRentCity
		},
		getChoosedRentCityStation(state) {
			return state.choosedRentCityStation
		},
		getRentInfo(state) {
			return state.rentInfo
		},
		// 获取 拼车起订人数
		getPinNumber(state) {
			return state.pinNumber
		},
		// 获取 需要支付的订单的类型状态
		getOrderPayType(state) {
			return state.orderPayType
		},
		// 获取邀请码
		getInviteCode(state) {
			return state.inviteCode
		},
		// 是否需要登录
		getIsLogin(state) {
			return state.isLogin
		},
		// 是否需要弹出优惠券
		getIsCouponTan(state) {
			return state.isCouponTan
		},
		// 获取版本
		getIsCheck(state) {
			return state.isCheck
		},
		// 网络静态图片
		www(state) {
			return state.www
		},
		// 网络图片
		wwwImgUrl(state) {
			return state.wwwImgUrl
		},
		// 获取切换的当前城市
		getChangeNowCity(state) {
			return state.changeNowCity
		},
		// 获取用户积分信息
		getCustomerInfo(state) {
			return state.customerInfo || uni.getStorageSync('customerInfo')
		},
		// 获取选取的景区列表
		getJingqulist(state) {
			return state.jingqulist
		},
		// 获取选取的酒店列表
		getJiudianlist(state) {
			return state.jiudianlist
		},
		// 获取选取的商品列表
		getShoplist(state) {
			return state.shoplist
		},
		// 获取文章发布搜索历史
		getHistorylist(state) {
			return state.historylist
		},
		// 获取 定制选择的城市
		getChoosedMakeCity(state) {
			return state.choosedMakeCity
		},
		// 获取 购物车的数据
		getnowmyshop(state) {
			return state.nowmyshop
		},
		// 获取 购物车的数据
		getselected_list(state) {
			return state.selected_list
		},
		
	},
	actions: {
		getUserAuthorizeStatus() {
			return new Promise((resolve, reject) => {
				$http.get($api.getAuthorizeStatus).then(res => {
					resolve(res)
				})
			})
		},
		upcustomerInfo(context, data) {
			context.commit('upCustomerInfo', data)
		},
	}
})

export default store
